﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.SessionState;
using Rison.DataAccess.Data.ZJCP;
using Rison.BusinessFacade.Common;

namespace Rison.Web.ZJCP.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SaveIllustrativecase : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            if (!CurrentLoginedUser.Logined)
            {
                context.Response.Redirect("../../Login.aspx");
            }
            else
            {
                string caseNum = context.Request.Params["caseNum"];
                string caseTime = context.Request.Params["caseTime"];
                string caseReason = context.Request.Params["caseReason"];
                string caseResult = context.Request.Params["caseResult"];
                string departmentId = context.Request.Params["departmentId"];
                string ilId = context.Request.Params["ilId"];
                if (!String.IsNullOrEmpty(departmentId) && !String.IsNullOrEmpty(caseNum) && !String.IsNullOrEmpty(caseTime) && !String.IsNullOrEmpty(caseReason) && !String.IsNullOrEmpty(caseResult))
                {
                    if (String.IsNullOrEmpty(ilId))
                    {
                        insertIllustrativecase(caseNum, caseTime, caseReason, caseResult, departmentId);
                    }
                    else
                    {
                        updateIllustrativecase(caseNum, caseTime, caseReason, caseResult, ilId);
                    }
                    
                }
                context.Response.Redirect(String.Format("../IllustrativecaseList.aspx?departmentId={0}",departmentId));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private Boolean updateIllustrativecase(string caseNum, string caseTime, string caseReason, string caseResult, string ilId)
        {
            string sql = String.Format("update Illustrativecase set il_case_num = '{0}', il_case_time = '{1}', il_case_reason = '{2}', il_case_result = '{3}', il_last_save_time = getdate()  where il_id = '{4}'", caseNum, caseTime, caseReason, caseResult, ilId);
            return DbAccess.executeUpdate(sql) == 1;
        }
        private Boolean insertIllustrativecase(string caseNum, string caseTime, string caseReason, string caseResult, string departmentId)
        {
            string sql = String.Format("insert into Illustrativecase values(newid(), '{0}', '{1}', '{2}', '{3}', '{4}', getdate())", departmentId, caseNum, caseTime, caseReason, caseResult);
            return DbAccess.executeUpdate(sql) == 1;
        }
    }
}
